﻿#include "registerform.h"
#include "ui_registerform.h"
#include <QMessageBox>
#include <QDebug>

RegisterForm::RegisterForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::RegisterForm)
{
    ui->setupUi(this);
    for(size_t i = 0; i < 4; i++)
    {
        m_isOkToRegister[i] = false;
    }
}

RegisterForm::~RegisterForm()
{
    delete ui;
}


void RegisterForm::on_pb_finish_clicked()
{
    QStringList parameters;
    for(size_t i = 0; i < 4; i++)
    {
        if (!m_isOkToRegister[i])
        {
            QMessageBox::warning(this,
                         "警告",
                         "将信息填写完善",
                         QMessageBox::Yes,
                         QMessageBox::Yes);
            return;
        }
    }

    parameters.append(ui->le_userID->text());
    parameters.append(ui->le_password->text());
    parameters.append(QString::number(ui->cb_role->currentIndex()));
    if (ui->cb_sex->currentText() == "保密")
    {
        parameters.append("");
    }else
    {
        parameters.append(ui->cb_sex->currentText());
    }
    parameters.append(ui->le_name->text());
    parameters.append(ui->le_phone->text());
    parameters.append(ui->de_birth->text());

    emit signalUserRegister(parameters);
}

void RegisterForm::on_pb_back_clicked()
{
    this->hide();
}

///匹配英文/数字/
void RegisterForm::on_le_userID_textChanged(const QString &arg1)
{
    if (arg1.isEmpty())
    {
        ui->lb_userIDRemind->setText("*");
    }
    else
    {
        QRegExp password_reg_exp("^[A-Za-z0-9]+$");/// \w+  [\u4e00-\u9fa5]{0,}
        QRegExpValidator *password_validator = new QRegExpValidator(password_reg_exp);
        if(!password_validator->regExp().exactMatch(arg1))/// 匹配不对
        {
            m_isOkToRegister[REG_USERID] = false;
            ui->lb_userIDRemind->setText("X");
        }
        else
        {
            m_isOkToRegister[REG_USERID] = true;
            ui->lb_userIDRemind->setText("√");
        }
    }
}

///匹配英文字符+数字 6-20
void RegisterForm::on_le_password_textChanged(const QString &arg1)
{
    if (arg1.isEmpty())
    {
        ui->lb_passwordRemind->setText("*");
    }
    else
    {
        QRegExp password_reg_exp("^[A-Za-z0-9]{6,20}$");
        QRegExpValidator *password_validator = new QRegExpValidator(password_reg_exp);
        if(!password_validator->regExp().exactMatch(arg1))/// 匹配不对
        {
            ui->lb_passwordRemind->setText("X");
        }
        else
        {
            ui->lb_passwordRemind->setText("√");
        }
    }
}

void RegisterForm::on_le_passwordOK_textChanged(const QString &arg1)
{
    if (arg1.isEmpty())
    {
        ui->lb_passwordOKRemind->setText("*");
    }
    else
    {
        if(arg1 != ui->le_password->text())/// 匹配不对
        {
            m_isOkToRegister[REG_USERPSW] = false;
            ui->lb_passwordOKRemind->setText("X");
        }
        else
        {
            m_isOkToRegister[REG_USERPSW] = true;
            ui->lb_passwordOKRemind->setText("√");
        }
    }
}

///汉字 + 英文 + 数字 + 下划线
void RegisterForm::on_le_name_textChanged(const QString &arg1)
{
    if (arg1.isEmpty())
    {
        ui->lb_nameRemind->setText("*");
    }
    else
    {
        QRegExp password_reg_exp("^[\u4e00-\u9fa5]|\\w+$");///
        QRegExpValidator *password_validator = new QRegExpValidator(password_reg_exp);
        if(!password_validator->regExp().exactMatch(arg1))/// 匹配不对
        {
            m_isOkToRegister[REG_USERNAME] = false;
            ui->lb_nameRemind->setText("X");
        }
        else
        {
            m_isOkToRegister[REG_USERNAME] = true;
            ui->lb_nameRemind->setText("√");
        }
    }
}

void RegisterForm::on_le_phone_textChanged(const QString &arg1)
{
    if (arg1.isEmpty())
    {
        ui->lb_phoneRemind->setText("*");
    }
    else
    {
        QRegExp password_reg_exp("^13[0-9]{1}[0-9]{8}|^15[0-9]{1}[0-9]{8}|^18[0-9]{1}[0-9]{8}");///
        QRegExpValidator *password_validator = new QRegExpValidator(password_reg_exp);
        if(!password_validator->regExp().exactMatch(arg1))/// 匹配不对
        {
            m_isOkToRegister[REG_USERPHONE] = false;
            ui->lb_phoneRemind->setText("X");
        }
        else
        {
            m_isOkToRegister[REG_USERPHONE] = true;
            ui->lb_phoneRemind->setText("√");
        }
    }
}

